<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Intervention\Image\Facades\Image;

class StorageController extends Controller
{
    function image(Request $request)
    {
        $file = array(
            'image' => Input::file('file')
        );
        // 设置验证条件
        // mimes:jpeg,bmp,png and for max size max:10000
        $rules = array(
            'image' => 'required'
        );
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            return $this->returnJSON(null, 500, '图片上传失败');
        } else {
            Log::info(Input::file('file'));
            if (Input::file('file')->isValid()) {
                $destinationPath = base_path("public/uploads/".date('Y-m'));
                $extension = Input::file('file')->getClientOriginalExtension(); // getting image extension
                $fileName = rand(11111, 99999) . time() . '.' . $extension; // renameing image
                Input::file('file')->move($destinationPath, $fileName); // uploading file to given path
                $path = $destinationPath.'/'.$fileName;
                $im = Image::make($path);
                $quality = ( $compress = request()->get('compress', 'false')) == 'true' ? 70:100;
                $im->save($path, $quality);
                return $this->returnJSON([
                    'url' => $this->imageUrl('/uploads/' . date('Y-m') . '/' . $fileName),
                    'info' => [
                        'len' => $im->filesize()
                    ]
                ]);
            } else {
                return $this->returnJSON(null, 500, '图片上传失败');
            }
        }
    }

    function image2(Request $request)
    {
        $file = array(
            'image' => Input::file('file')
        );
        // 设置验证条件
        // mimes:jpeg,bmp,png and for max size max:10000
        $rules = array(
            'image' => 'required'
        );
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            return $this->returnJSON(null, 500, '图片上传失败');
        } else {
            Log::info(Input::file('file'));
            if (Input::file('file')->isValid()) {
                $destinationPath = base_path("public/uploads/".date('Y-m'));
                $extension = Input::file('file')->getClientOriginalExtension(); // getting image extension
                $fileName = rand(11111, 99999) . time() . '.' . $extension; // renameing image
                Input::file('file')->move($destinationPath, $fileName); // uploading file to given path
                $path = $destinationPath.'/'.$fileName;
                $im = Image::make($path);
                $quality = ( $compress = request()->get('compress', 'false')) == 'true' ? 70:100;
                $im->save($path, $quality);

                $ret = [
                    'status' => 0,
                    'msg' => 'ok',
                    'link' => $this->imageUrl('/uploads/' . date('Y-m') . '/' . $fileName),
                ];

                return new JsonResponse($ret, 200);
            } else {
                return $this->returnJSON(null, 500, '图片上传失败');
            }
        }
    }

    function file()
    {
        $file = array(
            'file' => Input::file('file')
        );
        // 设置验证条件
        // mimes:jpeg,bmp,png and for max size max:10000
        $rules = array(
            'file' => 'required'
        );
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            return $this->returnJSON(null, 500, '文件上传失败');
        } else {
            // Log::info(Input::file('file'));
            if (Input::file('file')->isValid()) {
                $destinationPath = base_path("public/uploads");
                $fileName = Input::file('file')->getClientOriginalName();



                Input::file('file')->move($destinationPath, $fileName); // uploading file to given path
                return $this->returnJSON([
                    'url' => '/uploads/' . $fileName,
                    'filename' =>  $fileName,
                    'value' => '/uploads/' . $fileName
                ]);
            } else {
                return $this->returnJSON(null, 500, '文件上传失败');
            }
        }
    }

    public function imageUrl($url)
    {
        if(empty($url))        {
            return $url;
        }
        $path = parse_url($url);

        if(empty($path['host']))
        {
            $url = $url[0] == '/'? env('IMAGE_URL').$url:env('IMAGE_URL').'/'.$url;
        }

        return $url;
    }
}
